package com.example.demo.services;

import com.example.demo.entity.Location.Location;
import com.example.demo.entity.Location.LocationCode;
import com.example.demo.entity.Location.LocationDTO;
import com.example.demo.repository.LocationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class LocationService {
    @Autowired
    private LocationRepository locationRepo;

    public void updateLocation(List<LocationDTO> locations) {
        List<LocationCode> codes = locations.stream()
                .map(locationDTO -> {
                    LocationCode locationCode = new LocationCode();
                    locationCode.setWorkingCenterCode(locationDTO.getWorkingCenterCode());
                    locationCode.setWaLocationName(locationDTO.getLocationName());
                    return locationCode;
                }).toList();
        System.out.println("1" + codes);
        codes.forEach(locationCode -> {
            String waLocationCode = locationRepo.findCode(
                    locationCode.getWorkingCenterCode(),
                    locationCode.getWaLocationName()
            );
            locationCode.setWaLocationCode(waLocationCode);
        });
        System.out.println("2" + codes);

        Map<String, String> codeMap = codes.stream()
                .collect(Collectors.toMap(
                        LocationCode::getWaLocationName,
                        LocationCode::getWaLocationCode
                ));
        locations.forEach(locationDTO -> {
            String waLocationCode = codeMap.get(locationDTO.getLocationName());
            if (waLocationCode != null) {
                locationDTO.setLocationCode(waLocationCode);
            }
        });
        System.out.println("3" + locations);

        List<Location> locationList = locations.stream()
                .map(locationDTO -> {
                    Location location = new Location();
                    location.setId(locationDTO.getId());
                    location.setWorkingAreaCode(locationDTO.getWorkingAreaCode());
                    location.setLocationCode(locationDTO.getLocationCode());
                    location.setGoodsCode(locationDTO.getGoodsCode());
                    location.setBatchNo(locationDTO.getBatchNo());
                    location.setOnHandQty(locationDTO.getOnHandQty());
                    return location;
                }).toList();
        System.out.println("4" + locationList);

        for (Location location : locationList) {
            locationRepo.findById(location.getId()).ifPresent(existingLocation -> {
                existingLocation.setWorkingAreaCode(location.getWorkingAreaCode());
                existingLocation.setLocationCode(location.getLocationCode());
                existingLocation.setGoodsCode(location.getGoodsCode());
                existingLocation.setOnHandQty(location.getOnHandQty());
                System.out.println(existingLocation);
                locationRepo.save(existingLocation);
            });
        }
    }
    /*
        const totalQuantity = computed(() => {
  return warehouseCards.value.reduce((sum, card) => {
    const q = card.quantity || 0; // 如果 quantity 是 null，则视为 0
    return sum + q;
  }, 0);
});
[p_wa_location_stock]
         */

}
